package com.proj.hsy;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class ChildWebView extends Activity implements WebViewInterface {

	private WebView inAppWebView;

	public String CallbackId;

	@SuppressWarnings("deprecation")
	@SuppressLint({ "NewApi", "SetJavaScriptEnabled" })
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// WebView
		inAppWebView = (WebView) findViewById(R.id.webView);
		inAppWebView.setWebChromeClient(new WebChromeClient());
		WebViewClient client = new InAppBrowserClient(this, inAppWebView);
		inAppWebView.setWebViewClient(client);
		WebSettings settings = inAppWebView.getSettings();
		settings.setJavaScriptEnabled(true);
		settings.setJavaScriptCanOpenWindowsAutomatically(true);
		settings.setBuiltInZoomControls(true);

		settings.setPluginState(android.webkit.WebSettings.PluginState.ON);
		settings.setDomStorageEnabled(true);
		inAppWebView.loadUrl("file:///android_asset/www/"
				+ getIntent().getStringExtra("url"));
		inAppWebView.setId(6);
		inAppWebView.getSettings().setLoadWithOverviewMode(true);
		inAppWebView.getSettings().setUseWideViewPort(true);
		inAppWebView.requestFocus();
		inAppWebView.requestFocusFromTouch();
	}

	@Override
	public Activity getActivity() {
		return this;
	}

	@Override
	public Context getContext() {
		return this;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		try {
			if (requestCode == 0) {
				Bundle extras = data.getExtras();
				
				Bitmap b = (Bitmap) extras.get("data");
				if (extras == null || b == null) {
					System.out.println("error : camera picture");
					return;
				}
				String imageBase64 = ImageUtil.bitmapToBase64(b);
				inAppWebView.loadUrl("javascript:callback_pool['" + CallbackId
						+ "']('" + imageBase64 + "')");
			} else if (requestCode == 1) {
				Uri originalUri = data.getData(); //获得图片的uri
				if(originalUri == null){
					System.out.println("error : get picture");
					return;
				}
				
				ContentResolver resolver = getContentResolver();
				Bitmap b = MediaStore.Images.Media.getBitmap(resolver, originalUri); //显得到bitmap图片
				String imageBase64 = ImageUtil.bitmapToBase64(b);
				inAppWebView.loadUrl("javascript:callback_pool['" + CallbackId
						+ "']('" + imageBase64 + "')");
			}
		} catch (Exception e) {
		}
	}

	@Override
	public void setCallbackId(String callbackId) {
		this.CallbackId = callbackId;
	}
	
	@Override
	public String getViewName() {
		return "ChildActivity";
	}

}
